package evidence.view;

import evidence.controler.IControler;
import evidence.model.Model;
import evidence.model.modeloveTridy.DenniZaznam;
import evidence.model.modeloveTridy.TypSluzby;
import evidence.model.pomocneTridy.Time;
import evidence.model.pomocneTridy.TimeException;
import evidence.view.pomocneTridy.DateSpinnerModel;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.SpinnerDateModel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Protheo
 */
public class View extends JFrame implements IAbstractView, IPath {

    public static final long serialVersionUID = 1;
    private IControler controler;
    private Time pocatek;
    private Map<Integer, String> nast;
    private int selectedRow = -1;
    private String[] m28 = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28"};
    private String[] m29 = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29"};
    private String[] m30 = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"};
    private String[] m31 = new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"};

    /** Creates new form View
     * @param controler 
     */
    public View(IControler controler) {
        try {
            UIManager.setLookAndFeel(new com.sun.java.swing.plaf.windows.WindowsLookAndFeel());
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(View.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("chyba4");
        }
        initComponents();
        initTimeSpinners();
        this.setVisible(true);
        this.controler = controler;
        nast = this.controler.getSettings();
        this.setPanelVisible(false);
        SelectionListener listener = new SelectionListener(dayTable, this);
        this.dayTable.getSelectionModel().addListSelectionListener(listener);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        day = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        startTime = new javax.swing.JSpinner();
        endTime = new javax.swing.JSpinner();
        client = new javax.swing.JComboBox();
        ukon = new javax.swing.JComboBox();
        prestavka = new javax.swing.JCheckBox();
        ujeto = new javax.swing.JFormattedTextField();
        typUkonu = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        nepritomnost = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        addLine = new javax.swing.JButton();
        delete = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        dayTable = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        file = new javax.swing.JMenu();
        novy = new javax.swing.JMenuItem();
        open = new javax.swing.JMenuItem();
        ulozit = new javax.swing.JMenuItem();
        export = new javax.swing.JMenuItem();
        tisk = new javax.swing.JMenuItem();
        konec = new javax.swing.JMenuItem();
        settings = new javax.swing.JMenu();
        nastaveni = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Evidence pracovní doby");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        day.setName("day"); // NOI18N
        day.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                daySelected(evt);
            }
        });

        jLabel1.setText("Den:");

        endTime.setNextFocusableComponent(client);

        client.setEditable(true);
        client.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                klientSelected(evt);
            }
        });

        ukon.setEditable(true);
        ukon.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                ukonSelected(evt);
            }
        });

        prestavka.setText("Přestávka");

        ujeto.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(java.text.NumberFormat.getIntegerInstance())));
        ujeto.setText("0");

        jLabel2.setText("Začátek:");

        jLabel3.setText("Konec");

        jLabel4.setText("Klient:");

        jLabel5.setText("Úkon:");

        jLabel6.setText("Typ:");

        jLabel7.setText("Ujeto:");

        jLabel8.setText("Nepřítomnost:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(prestavka, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(startTime, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(endTime, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE))
                        .addGap(29, 29, 29)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(ukon, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(client, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(35, 35, 35)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel7))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(ujeto, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(nepritomnost, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(typUkonu, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(day, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(55, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(day, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(startTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(endTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel3))
                            .addComponent(jLabel5))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(prestavka)
                            .addComponent(jLabel6)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(client, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7)
                            .addComponent(ujeto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(nepritomnost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(ukon, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(typUkonu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        addLine.setText("Přidej / Změň");
        addLine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addLineActionPerformed(evt);
            }
        });

        delete.setText("Smaž");
        delete.setEnabled(false);
        delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(addLine, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(delete, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(374, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addLine)
                    .addComponent(delete))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        dayTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Datum", "OD", "Přestávka", "DO", "Služba", "Client", "Úkon", "Najeto", "Nepřítomnost"
            }
        ) {
            public static final long serialVersionUID = 1;
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        dayTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(dayTable);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 614, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 594, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 162, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        file.setText("Soubor");

        novy.setText("Nový");
        novy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                novyActionPerformed(evt);
            }
        });
        file.add(novy);

        open.setText("Otevřít");
        open.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openActionPerformed(evt);
            }
        });
        file.add(open);

        ulozit.setText("Uložit");
        ulozit.setEnabled(false);
        ulozit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ulozitActionPerformed(evt);
            }
        });
        file.add(ulozit);

        export.setText("Export");
        export.setEnabled(false);
        export.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exportActionPerformed(evt);
            }
        });
        file.add(export);

        tisk.setText("Tisk");
        tisk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tiskActionPerformed(evt);
            }
        });
        file.add(tisk);

        konec.setText("Konec");
        konec.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                konecActionPerformed(evt);
            }
        });
        file.add(konec);

        jMenuBar1.add(file);

        settings.setText("Nastavení");

        nastaveni.setText("Nastavení programu");
        nastaveni.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nastaveniActionPerformed(evt);
            }
        });
        settings.add(nastaveni);

        jMenuBar1.add(settings);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(12, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void novyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_novyActionPerformed
        NewMonth dotaz = new NewMonth(this, true);
        dotaz.setVisible(true);
    }//GEN-LAST:event_novyActionPerformed

    private void ulozitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ulozitActionPerformed
        if (!this.controler.isSaveFile()) {
            SaveOpen uloz = SaveOpen.getInstance(SaveOpen.SAVEDLG, this);
            uloz.setVisible(true);
        } else {
            this.save(null);
        }
    }//GEN-LAST:event_ulozitActionPerformed

    private void tiskActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tiskActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tiskActionPerformed

    private void konecActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_konecActionPerformed
        this.processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
    }//GEN-LAST:event_konecActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        if (!this.controler.isSaveFile() && !this.controler.isSaved()) {
            SaveOpen uloz = SaveOpen.getInstance(SaveOpen.SAVEDLG, this);
            uloz.setVisible(true);
        }else {
            this.save(null);
        }
        this.controler.saveAll();
    }//GEN-LAST:event_formWindowClosing

    @SuppressWarnings("deprecation")
    private void addLineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addLineActionPerformed
        try {
            Time zacatek = new Time(((Date) startTime.getValue()).getHours(), ((Date) startTime.getValue()).getMinutes());
            Time tkonec = new Time(((Date) endTime.getValue()).getHours(), ((Date) endTime.getValue()).getMinutes());
            String tukon = (String) typUkonu.getSelectedItem();
            Integer den = day.getSelectedIndex();
            String klient = (String) client.getSelectedItem();
            String uukon = (String) ukon.getSelectedItem();
            String neprit = nepritomnost.getText();
            Boolean prest = prestavka.isSelected();
            Integer dopr = null;
            try {
                dopr = ((Number) ujeto.getValue()).intValue();
            } catch (NullPointerException ex) {
                dopr = 0;
            }
            if (this.selectedRow==-1){
            controler.addDenniZaznam(den, zacatek, tkonec, tukon, klient, uukon, dopr, prest, neprit);
            } else {
                Map<Integer, Object> prop = new HashMap<Integer, Object>();
                prop.put(DenniZaznam.PROP_POC, zacatek);
                prop.put(DenniZaznam.PROP_KON, tkonec);
                prop.put(DenniZaznam.PROP_TYP, tukon);
                prop.put(DenniZaznam.PROP_KLI, klient);
                prop.put(DenniZaznam.PROP_UKO, uukon);
                prop.put(DenniZaznam.PROP_NEP, neprit);
                prop.put(DenniZaznam.PROP_DOP, dopr);
                prop.put(DenniZaznam.PROP_PRE, prest);
                controler.editDenniZaznam(den, this.selectedRow, prop);
                this.dayTable.getSelectionModel().clearSelection();
                this.selectedRow = -1;
            }
            this.showDay(controler.getDay(den));
            this.setTimeSpinnersValue(this.controler.getPosledni(den));
            this.prestavka.setEnabled(!this.controler.isPrestavka(den));
            this.prestavka.setSelected(false);
            this.showKlienti();
            this.ujeto.setValue(0);
            this.nepritomnost.setText("");
        } catch (TimeException ex) {
            Logger.getLogger(View.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_addLineActionPerformed

    private void deleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteActionPerformed
        this.controler.removeDenniZaznam(this.day.getSelectedIndex(), this.selectedRow);
        this.dayTable.getSelectionModel().clearSelection();
        this.delete.setEnabled(false);
        this.showDay(this.controler.getDay(this.day.getSelectedIndex()));
    }//GEN-LAST:event_deleteActionPerformed

    private void klientSelected(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_klientSelected
        this.showUkony((String) this.client.getSelectedItem());
    }//GEN-LAST:event_klientSelected

    private void ukonSelected(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_ukonSelected
        this.showTypy((String) this.ukon.getSelectedItem());
    }//GEN-LAST:event_ukonSelected

    private void daySelected(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_daySelected
        this.showDay(this.controler.getDay(this.day.getSelectedIndex()));
        this.setTimeSpinnersValue(this.controler.getPosledni(this.day.getSelectedIndex()));
    }//GEN-LAST:event_daySelected

    private void openActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openActionPerformed
        SaveOpen otevri = SaveOpen.getInstance(SaveOpen.OPENDLG, this);
        otevri.setVisible(true);
    }//GEN-LAST:event_openActionPerformed

    private void exportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportActionPerformed
        this.controler.convert();
    }//GEN-LAST:event_exportActionPerformed

    private void nastaveniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nastaveniActionPerformed
        Settings lnastaveni = new Settings(this, true, nast);
        lnastaveni.setVisible(true);
    }//GEN-LAST:event_nastaveniActionPerformed

    public void modelChaneged(PropertyChangeEvent evt) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addLine;
    private javax.swing.JComboBox client;
    private javax.swing.JComboBox day;
    private javax.swing.JTable dayTable;
    private javax.swing.JButton delete;
    private javax.swing.JSpinner endTime;
    private javax.swing.JMenuItem export;
    private javax.swing.JMenu file;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JMenuItem konec;
    private javax.swing.JMenuItem nastaveni;
    private javax.swing.JTextField nepritomnost;
    private javax.swing.JMenuItem novy;
    private javax.swing.JMenuItem open;
    private javax.swing.JCheckBox prestavka;
    private javax.swing.JMenu settings;
    private javax.swing.JSpinner startTime;
    private javax.swing.JMenuItem tisk;
    private javax.swing.JComboBox typUkonu;
    private javax.swing.JFormattedTextField ujeto;
    private javax.swing.JComboBox ukon;
    private javax.swing.JMenuItem ulozit;
    // End of variables declaration//GEN-END:variables

    public void newMonth(int rok, int mesic) {
        int dnu = this.controler.novyMonth(rok, mesic);
        this.setDayModel(dnu);
        this.showKlienti();
        this.setPanelVisible(true);
        this.menuEnabled(true);
    }

    private void showDay(List<List<Object>> list) {
        DefaultTableModel model = (DefaultTableModel) this.dayTable.getModel();
        int k = model.getRowCount();
        for (int i = 0; i < k; i++) {
            model.removeRow(0);
        }
        for (int i = 0; i < list.size(); i++) {
            List<Object> list1 = list.get(i);
            model.addRow(list1.toArray());
        }
    }

    private void showUkony(String klient) {
        List<String> uukony = this.controler.getUkony(klient);
        if (!uukony.isEmpty()) {
            String[] poleUkonu = new String[uukony.size()];
            uukony.toArray(poleUkonu);
            ukon.setModel(new DefaultComboBoxModel(poleUkonu));
            this.showTypy(uukony.get(0));
        } else {
            this.showTypy(null);
        }
    }

    private void showTypy(String ukon) {
        List<String> typy = null;
        if (ukon != null && !ukon.equals("")) {
            typy = TypSluzby.getSluzby(this.controler.getTyp(ukon));
        } else {
            typy = TypSluzby.getSluzby();
        }
        String[] poleTypu = new String[TypSluzby.pocetSluzeb()];
        typy.toArray(poleTypu);
        this.typUkonu.setModel(new DefaultComboBoxModel(poleTypu));
    }

    public void updateClient() {
    }

    public void open(File f) {
        int dnu = this.controler.openMonth(f);
        if (dnu != -1) {
            this.setDayModel(dnu);
            this.showKlienti();
            this.setPanelVisible(true);
            this.menuEnabled(true);
            this.showDay(this.controler.getDay(0));
            this.setTimeSpinnersValue(this.controler.getPosledni(0));
            this.prestavka.setEnabled(!this.controler.isPrestavka(0));
            this.prestavka.setSelected(false);
        }
    }

    public void save(File f) {
        this.controler.saveMonth(f);
    }

    @SuppressWarnings("deprecation")
    public Date setTime(Time cas) {
        Date time = new Date();
        time.setHours(cas.getHodiny());
        time.setMinutes(cas.getMinuty());
        return time;
    }

    private void initTimeSpinners() {
        try {
            this.pocatek = new Time(7, 30);
        } catch (TimeException ex) {
            Logger.getLogger(View.class.getName()).log(Level.SEVERE, null, ex);
        }
        SpinnerDateModel model = new DateSpinnerModel();
        startTime.setModel(model);
        startTime.setEditor(new javax.swing.JSpinner.DateEditor(startTime, "H:mm"));
        SpinnerDateModel model1 = new DateSpinnerModel();
        endTime.setModel(model1);
        endTime.setEditor(new javax.swing.JSpinner.DateEditor(endTime, "H:mm"));
        this.setTimeSpinnersValue(pocatek);
    }

    private void setTimeSpinnersValue(Time cas) {
        startTime.setValue(this.setTime(cas));
        endTime.setValue(startTime.getNextValue());
    }

    private void setPanelVisible(boolean visible) {
        this.jPanel1.setVisible(visible);
        this.jPanel2.setVisible(visible);
        this.jPanel3.setVisible(visible);
    }

    private void setDayModel(int dnu) {
        DefaultComboBoxModel cmodel;
        switch (dnu) {
            case 29: {
                cmodel = new DefaultComboBoxModel(m29);
            }
            break;
            case 30: {
                cmodel = new DefaultComboBoxModel(m30);
            }
            break;
            case 31: {
                cmodel = new DefaultComboBoxModel(m31);
            }
            break;
            default: {
                cmodel = new DefaultComboBoxModel(m28);
            }
        }
        this.day.setModel(cmodel);
    }

    private void showKlienti() {
        Object[] klienti = controler.getKlienti().toArray();
        this.client.setModel(new DefaultComboBoxModel(klienti));
        this.showUkony((String) this.client.getSelectedItem());
    }

    private void menuEnabled(boolean enable) {
        this.ulozit.setEnabled(enable);
        this.export.setEnabled(enable);
    }

    private class SelectionListener implements ListSelectionListener {

        private JTable table;
        private View okno;

        public SelectionListener(JTable table, View okno) {
            this.table = table;
            this.okno = okno;
        }

        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()&&table.getSelectedRow()!=-1) {
                okno.loadSelectedRow();
            } else {
            }
        }
    }

    public void loadSelectedRow() {
        if (this.selectedRow == -1) {
            this.delete.setEnabled(true);
        }
        this.selectedRow = this.dayTable.getSelectedRow();
        List<Object> seznam = this.controler.getDay(this.day.getSelectedIndex()).get(this.selectedRow);
        for (int i = 0; i < seznam.size(); i++) {
            Object string = seznam.get(i);
            switch (i) {
                case 1: {
                    try {
                        this.startTime.setValue(this.setTime(new Time((String)string)));
                    } catch (TimeException ex) {
                        this.startTime.setValue(new Date());
                    }
                }
                break;
                case 2: {
                    if(!string.equals("")){
                        this.prestavka.setSelected(true);
                        this.prestavka.setEnabled(true);
                    } else {
                        this.prestavka.setSelected(false);
                        this.prestavka.setEnabled(!this.controler.isPrestavka(this.day.getSelectedIndex()));
                    }
                }
                break;
                case 3: {
                    try {
                        this.endTime.setValue(this.setTime(new Time((String)string)));
                    } catch (TimeException ex) {
                        this.endTime.setValue(new Date());
                    }
                }
                break;
                case 4: {
                    this.typUkonu.setSelectedItem(string);
                }
                break;
                case 5: {
                    this.client.setSelectedItem(string);
                }
                break;
                case 6: {
                    this.ukon.setSelectedItem(string);
                }
                break;
                case 7: {
                    this.ujeto.setText(string.toString());
                }
                break;
                case 8: {
                    this.nepritomnost.setText(string.toString());
                }
            }
            this.typUkonu.setSelectedItem(seznam.get(4));
        }
    }

    public File getPath(){
        File f = new File(this.nast.get(Model.SETTINGS_EVI));
        if (!f.exists()){
            f = new File(".");
        }
        return f;
    }
}
